package chan.module.project.hw.mrs.hive2.v350.test;

import chan.module.project.comm.ConnectionCommonUtil;
import chan.module.project.comm.PropertiesUtil;

import java.sql.*;

public class MRSHive2V350TestMain {

    // 数据库连接信息
    private static String URL = "";
    private static String USER = "";
    private static String PASSWORD = "";

    public static void main(String[] args) throws Exception {
        PropertiesUtil util = PropertiesUtil.readConfig(args);
        URL = util.get("hive2_url");
        String query_sql = util.get("query_sql");
        String show_field = util.get("show_field");
        String[] fields = show_field.split(",");
        Connection conn = null;
        try {
            // 1. 加载驱动
            Class.forName("org.apache.hive.jdbc.HiveDriver");

            // 2. 建立连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            // 3. 增删改查操作
            // 插入数据
            //insert(conn, "张三", 25);

            // 查询数据
            query(conn,query_sql,fields);

            // 更新数据
            //update(conn, 1, "李四", 30);

            // 删除数据
            //delete(conn, 1);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 4. 关闭连接
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    // 查询方法
    public static void query(Connection conn,String sql,String[] fields) throws  Exception {
        //String sql = "DESCRIBE FORMATTED test_simple_field_12345";
        Statement stmt = null;
        ResultSet rs = null;
        int i = 0;
        try {
            StringBuilder resultSb = new StringBuilder();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                i++;
                for (String field : fields){
                    resultSb.append("col_name: ").append(field).append(" / value : ").append(  rs.getObject(field) );
                }
                resultSb.append("==== row number : ").append(i).append(" ====\n");
                System.out.println( resultSb );
                resultSb.setLength(0);
            }
        }catch (Exception e){
            System.out.println("row all size : " + i);
            throw e;
        }finally {
            ConnectionCommonUtil.close(rs,stmt,null);
        }
    }

    // 插入方法
    public static void insert(Connection conn, String name, int age) throws SQLException {
        String sql = "INSERT INTO users(name, age) VALUES(?, ?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setInt(2, age);
        int count = pstmt.executeUpdate();
        System.out.println("插入了 " + count + " 条记录");
        pstmt.close();
    }

    // 更新方法
    public static void update(Connection conn, int id, String name, int age) throws SQLException {
        String sql = "UPDATE users SET name=?, age=? WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setInt(2, age);
        pstmt.setInt(3, id);
        int count = pstmt.executeUpdate();
        System.out.println("更新了 " + count + " 条记录");
        pstmt.close();
    }

    // 删除方法
    public static void delete(Connection conn, int id) throws SQLException {
        String sql = "DELETE FROM users WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);
        int count = pstmt.executeUpdate();
        System.out.println("删除了 " + count + " 条记录");
        pstmt.close();
    }
}